package com.pwc.conversionTool.controller;


import java.util.Arrays;
import java.util.Locale;

import javax.servlet.ServletContext;

import org.dom4j.DocumentException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.pwc.conversionTool.Manager.SoapUIManager;
import com.pwc.conversionTool.Manager.XMLConfigImpl;


@Controller
public class ConfigController {
	@Autowired
	public ServletContext context;	
	
	private static final Logger logger = LoggerFactory.getLogger(ConfigController.class);
	


	
	@RequestMapping(value = "/config/XMLConfig", method = RequestMethod.GET)
	public String XMLConfig(Locale locale, Model model) {
		logger.error("log4j is running in XMLConfig");
		return "config/XMLconfig";
	}
	
	
	@RequestMapping(value = "/config/soapSecurity", method = RequestMethod.GET)
	public String userNamePasswordConfig(Locale locale, Model model) {
		
		try {
			XMLConfigImpl configImpl=XMLConfigImpl.initial(context);
			 String name = configImpl.getUserName();
		     String password= configImpl.getPassword();
		     String passwordType=configImpl.getPasswordType();
		     
		     model.addAttribute("name", name);
             model.addAttribute("password", password);
             model.addAttribute("passwordType", passwordType);
			
		} catch (Exception e) {
			logger.error("log4j is running in soapSecurity");
			logger.error(Arrays.toString(e.getStackTrace()));
		}
		return "config/soapSecurity";
	}
	
	@RequestMapping(value = "/config/wsdl", method = RequestMethod.GET)
	public String XMLEndpoint(Locale locale, Model model) throws DocumentException {
		
		try{
			if(SoapUIManager.isInitial())
			{
				model.addAttribute("msg", "server connect to endpoint:"+SoapUIManager.getUrl());
			}else {
				model.addAttribute("msg", "server is not connecting......");
			}		
			XMLConfigImpl xmlConfigImpl=XMLConfigImpl.initial(context);
			model.addAttribute("endpoint", xmlConfigImpl.getEndpoint());
			
		} catch (DocumentException e){
			logger.error("log4j is running in wsdl");
			logger.error(Arrays.toString(e.getStackTrace()));
		}
		return "config/xmlWSDL";
	}
	
	
	@RequestMapping(value = "/config/folderManager", method = RequestMethod.GET)
	public String XMLuploadpath(Locale locale, Model model) throws DocumentException {
	
		
		return "config/folderManager";
	}

	
	
}
